package com.jiwei.system.controller;

import com.jiwei.system.pojo.ResultPoJo;
import com.jiwei.system.vo.Login;
import com.jiwei.system.service.LoginService;
import com.jiwei.system.serviceImpl.SMSService;
import com.jiwei.system.utils.CookieUtil;
import com.jiwei.system.vo.ResultVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @title: UserLoginController
 * @Author 赖志宇
 * @Description TODO
 * @Date: 2020/8/18
 * @Version 1.0
 */
@RestController
@RequestMapping(value = "repair", method = RequestMethod.POST)
public class UserLoginController {

    @Resource(name = "SMSService")
    private SMSService smsService;

    @PostMapping(value = "getSMSCode")
    public ResultVO sendMessage(@RequestParam("telephone") String phoneNumber, HttpSession session) {
        System.out.println(phoneNumber);
        ResultPoJo resultPoJo = smsService.sendSMSCode(phoneNumber);
        if (resultPoJo.getStatusCode().equals("200"))
            session.setAttribute("login", resultPoJo.getContent());
        return resultPoJo.changeToVO();
    }

    @PostMapping(value = "login")
    public ResultVO login(@RequestParam("telephone") String phoneNumber, @RequestParam("Pwd") String code, @RequestParam("method") String method, HttpServletResponse response, HttpServletRequest request){

        LoginService loginService = LoginService.userLoginMethodMap.get(method);
        ResultPoJo resultPoJo = loginService.login(new Login(phoneNumber,code));
        if(resultPoJo.getStatusCode().equals("200"))
       CookieUtil.setMemberCookie(response, (String) resultPoJo.getContent());
            request.getSession().setAttribute("userlogin", phoneNumber);
        return resultPoJo.changeToVO();
    }

}
